package com.betterwood.dragon.controllers;

import com.betterwood.dragon.core.common.JsonResult;
import com.betterwood.dragon.core.common.PageApiResult;
import com.betterwood.dragon.dao.ResultDao;
import com.betterwood.dragon.dao.SearchDao;
import com.betterwood.dragon.entity.Result;
import com.betterwood.dragon.entity.Search;
import com.betterwood.dragon.entity.Site;
import com.betterwood.dragon.entity.dto.IdDTO;
import com.betterwood.dragon.entity.dto.SearchDTO;

import com.betterwood.dragon.service.SearchService;
import com.betterwood.dragon.service.SiteService;
import com.betterwood.dragon.service.WormNest;
import com.betterwood.dragon.utils.RedisUtil;
import com.betterwood.dragon.utils.TimeUtil;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import org.springframework.web.bind.annotation.CrossOrigin;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping(value = "/search")
@CrossOrigin
// 使用Lombok自动生成注入的构造函数
@RequiredArgsConstructor
@Slf4j
public class SearchController {

    private final SearchService searchService;

    private final ResultDao resultDao;
    private final SearchDao searchDao;

    // private final RedisTemplate<String, String> template;

    // @Resource(name = "redisTemplate")
    // private ListOperations<String, String> listOps;

    @PostMapping(value = "/searchAll")
    public PageApiResult<Result> searchAll(@RequestBody SearchDTO search) {
        Integer currentPage = ObjectUtil.isNull(search.getCurrentPage()) ? 0 : search.getCurrentPage() - 1;
        Integer pageSize = ObjectUtil.isNull(search.getPageSize()) ? 30 : search.getPageSize();
        Pageable pageAble = PageRequest.of(currentPage, pageSize, Sort.Direction.ASC, "price");

        String searchId = search.getSearchId();
        Search se = searchDao.getOneBySearchId(searchId);

        if (ObjectUtil.isNotNull(se)) {
            // return PageApiResult.page(resultDao.findBySearchId(searchId, pageAble));
            log.info("HEHRE  is LOG");
            search.setId(se.getId());
            log.info(search.toString());
            return PageApiResult.page(resultDao.findBySearchDto(search, pageAble));
        }

        if (StrUtil.equals(search.getKeywordStr(), "EMPTY")) {
            return PageApiResult.emptyList();
        }

        Search newSearch = new Search();
        newSearch.setSearchId(search.getSearchId());
        newSearch.setKeywordStr(search.getKeywordStr());
        newSearch.setScanTime(TimeUtil.currentTime());
        Search saved = searchDao.save(newSearch);
        search.setId(saved.getId());
        searchService.searchAll(search);

        return PageApiResult.page(resultDao.findBySearchId(searchId, pageAble));
    }

    @PostMapping(value = "/searchSingle")
    public JsonResult<String> searchSingle(@RequestBody SearchDTO search) {

        Search newSearch = new Search();
        newSearch.setSearchId(search.getSearchId());
        newSearch.setKeywordStr(search.getKeywordStr());
        // searchDao.save(newSearch);
        searchService.searchSingle(search);

        return JsonResult.success("success");
    }

    @PostMapping(value = "/searchList")
    public PageApiResult<Search> searchList(@RequestBody SearchDTO search) {
        log.info("***********");
        log.info(search.toString());
        log.info("***********");
        Integer currentPage = ObjectUtil.isNull(search.getCurrentPage()) ? 0 : search.getCurrentPage() - 1;
        Integer pageSize = ObjectUtil.isNull(search.getPageSize()) ? 30 : search.getPageSize();
        Pageable pageAble = PageRequest.of(currentPage, pageSize, Sort.Direction.ASC, "id");

        // String searchId = search.getSearchId();
        // Search se = searchDao.getOneBySearchId(searchId);

        // return PageApiResult.page(resultDao.findBySearchId(searchId, pageAble));
        log.info("INSIDE CACHE LIST");
        return PageApiResult.page(searchDao.findAll(pageAble));

    }

    @PostMapping(value = "/deleteSearch")
    public JsonResult<String> deleteSearch(@RequestBody SearchDTO search) {
        String id = search.getId();
        // Search searchRecord = searchService.getOneById(id);

        resultDao.deleteBySearchId(id);
        searchService.deleteById(id);
        // Search se = searchDao.getOneBySearchId(searchId);

        // return PageApiResult.page(resultDao.findBySearchId(searchId, pageAble));

        return JsonResult.success(search.getId());

    }

    @PostMapping(value = "/mockResult")
    public JsonResult mockResult(@RequestBody SearchDTO search) {
        for (int i = 0; i < 10; i++) {
            Result re = new Result();
            re.setKeywordStr("飞机餐");
            // re.setKeywordStr(search.getKeywordStr());
            re.setProductName("洋葱圈");
            re.setOrderUrl("http://www.baidu.com");
            re.setSearchId(search.getKeywordStr());
            // re.setSearchId("533123");
            re.setSiteName("福济");
            re.setSiteId("60a349b22432576e74236261");
            re.setSiteNo("FJ");
            re.setScanPrice("12");
            re.setStatus(0);

            resultDao.save(re);
        }

        for (int i = 0; i < 22; i++) {
            Result re = new Result();
            re.setKeywordStr("火车餐");
            re.setProductName("小面包");
            re.setOrderUrl("http://www.google.com");
            re.setSearchId(search.getSearchId());
            re.setSiteName("扬帆");
            re.setSiteId("60a5ebefe6173d5a75211fab");
            re.setSiteNo("YF");
            re.setScanPrice("5");
            re.setStatus(0);
            resultDao.save(re);
        }
        return JsonResult.success("OK");
    }
}
